# _*_coding:utf-8_*_

from ATS.instrumentDriver import InstGen

OK = 0
debug_mode = False


class SA_N9000(InstGen.SA):
    def __init__(self, name, info_dict):
        InstGen.SA.__init__(self, name, info_dict)
        print(name, info_dict)

    def __universalcom(self, cmdlist, func_id=0):
        """
        func_id: 0="write",1="read",2="ask",3="ask for values"
        """
        rval = None
        if not debug_mode:
            for cmd_str in cmdlist:
                if func_id == 0:
                    self.handler.write(cmd_str)
                elif func_id == 1:
                    rval = self.handler.read()
                elif func_id == 2:
                    rval = self.handler.ask(cmd_str)
                elif func_id == 3:
                    rval = self.handler.ask_for_values(cmd_str)
                elif func_id == 4:
                    rval = self.handler.read_values()
        else:
            for cmd in cmdlist:
                print("\n ", self.name, cmd)
                if func_id == 0:
                    self.handler.write(cmd)
                    if "?" not in cmd:
                        err = self.handler.ask(":syst:err?")
                        print("###error: ", err)
                elif func_id == 1:
                    rval = self.handler.read()
                elif func_id == 2:
                    rval = self.handler.ask(cmd)
                elif func_id == 3:
                    rval = self.handler.ask_for_values(cmd)
                elif func_id == 4:
                    rval = self.handler.read_values()
                print("\t", rval)
        return rval

    def marker_set(self, mknumber=1, on_off=True, mk_x=1e09):
        cmd_list = []
        if on_off:
            # ss1 = ':CALC1:MARK%s ON' %mknumber
            cmd_list.append(':CALC1:MARK%s ON' % mknumber)
            cmd_list.append(':CALC1:MARK%s:X %s' % (mknumber, mk_x))
            self.__universalcom(cmd_list)
            # ":CALC1:SEL:MARK3:X? "
            cmd = [':CALC1:SEL:MARK%s:Y? ' % mknumber]
            # print(cmd)
            rval = self.__universalcom(cmd, 3)
            return rval
        else:
            ss1 = ':CALC1:MARK%s OFF' % mknumber
            cmd_list.append(ss1)
            self.__universalcom(cmd_list)
            return OK

    def marker_off(self, mknumber=1):
        cmd_list = []
        ss1 = ':CALC1:MARK%s OFF' % mknumber
        cmd_list.append(ss1)
        # cmd_list.append(":CALC1:MARK1 OFF")
        self.__universalcom(cmd_list)
        return OK

    def export_xdata(self):
        cmd = [":CALC1:SEL:DATA:XAX? "]
        rval = self.__universalcom(cmd, 3)
        return rval

    def export_ydata(self):
        cmd = [":CALC1:SEL:DATA:FDATa? "]
        rval = self.__universalcom(cmd, 3)
        index = 0
        ylist = []
        for index, ss in enumerate(rval):
            if index % 2 == 0:
                ylist.append(rval[index])
        return ylist

    def load_setup(self, stat_path):
        cmd = [":MMEM:LOAD:STAT '%s'" % stat_path]
        return OK
